<template>
  <el-dialog :visible.sync="visible" top="20px" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="140px">
      <el-form-item prop="name" :label="$t('name')">
        <el-input v-model="dataForm.name" :placeholder="$t('name')"/>
      </el-form-item>
      <el-form-item prop="qualificationCode" :label="$t('lawyer.qualificationCode')">
        <el-input v-model="dataForm.qualificationCode" :placeholder="$t('lawyer.qualificationCode')"/>
      </el-form-item>
      <el-form-item prop="qualificationGetDate" :label="$t('lawyer.qualificationGetDate')">
        <el-date-picker type="date" v-model="dataForm.qualificationGetDate" :placeholder="$t('lawyer.qualificationGetDate')" />
      </el-form-item>
      <el-form-item prop="practiceType" :label="$t('lawyer.practiceType')">
        <el-input v-model="dataForm.practiceType" :placeholder="$t('lawyer.practiceType')" />
      </el-form-item>
      <el-form-item prop="phone" :label="$t('phone')">
        <el-input v-model="dataForm.phone" :placeholder="$t('phone')" />
      </el-form-item>

      <el-form-item prop="lawOfficeId" :label="$t('lawyer.lawOfficeId')">
<!--        <el-input v-model="dataForm.lawOfficeId" :placeholder="$t('lawyer.lawOfficeId')" />-->
        <el-select
                v-model="dataForm.lawOfficeId"
                filterable
                remote
                reserve-keyword
                placeholder="请输入律所名称关键词"
                :remote-method="remoteMethod"
                :loading="loading"
                style="width: 100%"  >
          <el-option
                  v-for="item in lawOfficeList"
                  :key="item.id"
                  :label="item.orgName"
                  :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="position" :label="$t('lawyer.position')">
        <el-input v-model="dataForm.position" :placeholder="$t('lawyer.position')" />
      </el-form-item>
      <el-form-item prop="birthday" :label="$t('lawyer.birthday')">
        <el-date-picker type="date" v-model="dataForm.birthday" :placeholder="$t('lawyer.birthday')" />
      </el-form-item>
      <el-form-item prop="nation" :label="$t('lawyer.nation')">
        <el-input v-model="dataForm.nation" :placeholder="$t('lawyer.nation')" />
      </el-form-item>
      <el-form-item prop="email" :label="$t('lawyer.email')">
        <el-input v-model="dataForm.email" :placeholder="$t('lawyer.email')" />
      </el-form-item>
      <el-form-item prop="remark" :label="$t('remark')">
        <el-input v-model="dataForm.remark" :placeholder="$t('remark')"/>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { debounce } from 'lodash'
  export default {
    data () {
      return {
        visible: false,
        lawOfficeList: [],
        dataForm: {
          id: '',
          name: '',
          nation: '',
          birthday:'',
          position: '',
          practiceType: '',
          lawOfficeId: '',
          qualificationGetDate: '',
          qualificationCode: '',
          phone: '',
          address: '',
          email: '',
          remark: ''
        },
        loading: false,
      }
    },
    computed: {
      dataRule () {
        return {
          name: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          qualificationCode: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          Promise.all([

          ]).then(() => {
            if (this.dataForm.id) {
              this.getInfo()
            }
          })
        })
      },
      // 获取信息
      getInfo () {
        this.$axios.get(`/lawyer/info/${this.dataForm.id}`).then(res => {
          this.dataForm = {
            ...this.dataForm,
            ...res
          }
        }).catch(() => {})
      },
      // 表单提交
      dataFormSubmitHandle: debounce(function () {
        this.$refs['dataForm'].validate((valid) => {
          if (!valid) {
            return false
          }
          // this.dataForm.menuIdList = JSON.stringify(this.dataForm.menuIdList)
          this.$http.json('/lawyer/addOrUpdate', this.dataForm).then(res => {
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }).catch(() => {})
          // this.$axios[!this.dataForm.id ? 'post' : 'put']('/role/addRole', JSON.stringify(this.dataForm)).then(res => {
          //   this.$message({
          //     message: this.$t('prompt.success'),
          //     type: 'success',
          //     duration: 500,
          //     onClose: () => {
          //       this.visible = false
          //       this.$emit('refreshDataList')
          //     }
          //   })
          // }).catch(() => {})
        })
      }, 1000, { 'leading': true, 'trailing': false }),

      remoteMethod(query) {
        if (query !== '') {
          this.loading = true;
          this.getLawOfficeList(query);
          // setTimeout(() => {
          //   this.loading = false;
          //   this.options = this.list.filter(item => {
          //     return item.label.toLowerCase()
          //             .indexOf(query.toLowerCase()) > -1;
          //   });
          // }, 200);
        } else {
          this.lawOfficeList = [];
        }
      },

      // 获取律所列表
      getLawOfficeList(queryName) {

        return this.$axios.get('/lawOffice/page', {
          params: {
            order: 'ASC',
            orderField: 'createTime',
            current: 1,
            size: 20,
            listMode: true,
            queryItems: 'orgName,LIKE,' + queryName
          }
        }).then(res => {
          this.loading = false;
          this.lawOfficeList = res.records
        }).catch(() => {
        })
      }


    }
  }
</script>
